####
# leader survey
####

rm(list=ls())
packs = c('tidyverse', 'hrbrthemes', 'ggalt', 'ggrepel')
source("repFile/r/helpers.R")
loadPkg(packs)


# data
load("repFile/data/leader-survey.rda")


# theft
leader$stolen = recode(leader$`item stolen`, 
                          `Yes` = 1L, `No` = 0L, 
                          `no` = 0L,
                          `1` = 1L, `0` = 0L)

leader$conflict = recode(leader$cnflict, 
                       `Yes` = 1L, 
                       `No` = 0L, 
                       `yes` = 1L,
                       `no` = 0L,
                       `1` = 1L, `0` = 0L)

leader$years_leader = leader$`No. of years of leadership in the slum`

# dummy out chief role
leader$`Chief Role in the Slum` %>% table()
keys = c('problem*', 'challenge*', 'in need', 'basic service*')

leader$role_problem = 
  ifelse(str_detect(leader$`Chief Role in the Slum`, paste(keys, collapse='|')),1,0)


# plot
update_geom_defaults("label", list(family = "Roboto Condensed"))
update_geom_defaults("label_repel", list(family = "Roboto Condensed"))

leader %>% 
  select(role_problem, stolen, conflict) %>% 
  gather(key = var, value = value) %>% 
  group_by(var) %>% 
  summarise_all(funs(mean)) %>%
  mutate(var = fct_reorder(var, value)) %>% 
  mutate(label = c('Conflict between neighbors', 
                   'Primary role: \n basic services, \nlocal challenges', 
                   'A major item is stolen')) %>% 
  ggplot(data = ., aes(x = var, y = value, label = label)) + 
  geom_col(width = .5) + 
  coord_flip() + 
  theme_ipsum_rc(grid = "X") +
  scale_y_percent(limits=c(0,.8)) + 
  labs(x = '', y = 'Percent of leaders', 
       title = 'Role of Informal Leaders in Slums', 
       subtitle = "Key services that leaders reported providing for slum.") + 
  geom_label_repel(nudge_y = .05, segment.size = 0) + 
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

ggsave('repFile/paper/figures/leader-survey-roles.pdf', device = cairo_pdf)
ggsave('repFile/paper/figures/leader-survey-roles.tiff', device = "tiff")

